<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <!-- :model="form" 双向绑定对象 -->
      <el-form ref="form" status-icon :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
        </el-form-item>
        <el-form-item prop="agree">
          <el-checkbox v-model="form.agree">我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reqLogin } from '@/api/user.js'
import { setToken } from '@/utils/storage.js'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '13911111111',
        code: '246810',
        agree: 'true'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
          { pattern: /^1[3-8]\d{9}$/, message: '请输入合法手机号', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: ['change', 'blur'] },
          { pattern: /^\d{6}$/, message: '请输入6位验证码', trigger: ['blur', 'change'] }
        ],
        agree: [
          {
            validator: function (rule, value, callback) {
              if (value) {
                callback()
              } else {
                callback(new Error('请同意用户协议!'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    async login () {
      try {
        // 对整个表单进行校验
        await this.$refs.form.validate()
      } catch {
        return console.log('校验失败')
      }
      // 校验成功
      // 发送ajax请求
      try {
        const res = await reqLogin(this.form.mobile, this.form.code)
        // 调用api封装的请求 reqLogin(mobile,code)
        setToken(res.data.data.token)
        this.$message.success('登录成功')
        this.$router.push('/')
      } catch {
        this.$message.error('手机号或者验证码错误')
      }
    }
  }
}
</script>

 <style lang="less" >
 .login-container{
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: url("~@/assets/login_bg.jpg") no-repeat;
    background-size: cover;
    .login-form-wrap{
      min-width: 300px;
      padding: 30px 50px 10px;
      background-color: #fff;
      .login-head{
        display: flex;
        justify-content: center;
        .logo{
          width: 200px;
          height: 57px;
          background: url("~@/assets/logo_index.png") no-repeat;
          background-size: contain;
        }
      }
      .el-form{
        .el-button{
          width: 100%;
        }
      }
    }
}
 </style>
